package com.acme.bankapp.service.network;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

public class BankRemoteConsole {
	private String ip;
	private int port;

	public BankRemoteConsole(final String ip, final int port) {
		this.ip = ip;
		this.port = port;
	}

	public void startClientConsole() {
		System.out.println("Connecting to " + ip + ":" + port);

		try (Socket socket = new Socket(ip, port);
				ObjectOutputStream socketWriter = new ObjectOutputStream(socket.getOutputStream());
				ObjectInputStream socketReader = new ObjectInputStream(socket.getInputStream());) {
			String response, request;

			response = (String)socketReader.readObject();
			System.out.println(response);
			System.out.print(">");
			do {
				request = System.console().readLine();
				socketWriter.writeObject(request);
				response = (String) socketReader.readObject();
				System.out.print(response);
				System.out.print("\n>");
			} while (! "shutdown".equals(request));
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			System.out.println("couldn't convert to string");
		}
	}
}
